package com.example.exam4;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.exam4.adapter.PlanetAdapter;
import com.example.exam4.entity.Planet;

import java.util.ArrayList;
import java.util.List;

public class exam4 extends AppCompatActivity {
    private List<Planet> planetList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam4);
        //初始化Planet列表
        initPlanet();
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        //加载布局管理者
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //加载适配器
        PlanetAdapter adapter = new PlanetAdapter(planetList);
        recyclerView.setAdapter(adapter);
    }

    private void initPlanet() {
        Resources resources = getResources();
        //加载图片资源ID
        @SuppressLint("Recycle") TypedArray imgArr = resources.obtainTypedArray(R.array.planet_images);
        String[] titleArr = resources.getStringArray(R.array.planet_titles);
        String[] descArr = resources.getStringArray(R.array.planet_descriptions);
        Planet planet;
        for (int i = 0; i < imgArr.length(); i++) {
            planet = new Planet();
            planet.setPlaImg(imgArr.getResourceId(i, 0));
            planet.setPlaTitle(titleArr[i]);
            planet.setPlaDesc(descArr[i]);
            planetList.add(planet);
        }
    }
}
